package com.examples.maps;

import java.util.ArrayList;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngCreator;
import com.google.android.gms.maps.model.PolygonOptions;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Parcelable;

	public class dbShape extends SQLiteOpenHelper {
		public static final String DATABASE_NAME = "DB.db";
		public static final String DATA_TABLE_NAME = "shapeData";
		public static final int DATABASE_VERSION = 8;
		
		public static final String DATA_COLUMN_ID = "id";
		public static final String DATA_COLUMN_TYPE = "Type";
		public static final String DATA_COLUMN_POINT = "Point";
		public static final String DATA_COLUMN_NAME = "shapeName";
		
		public dbShape(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db){
			// TODO Auto-gennerated method stud
			db.execSQL("create table shapeData"
					+ "(id integer primary key, Type text, Point text, shapeName text)");
				
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS shapeData");
			this.onCreate(db);
		}
		
		public boolean insertShape(MyShape shape) {
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues contentValues = new ContentValues();
			
			contentValues.put(DATA_COLUMN_TYPE, shape.getType());
			contentValues.put(DATA_COLUMN_POINT, shape.getPoint());
			contentValues.put(DATA_COLUMN_NAME, shape.getName());
			
			db.insert(DATA_TABLE_NAME, null, contentValues);
			db.close();
			return true;
		}
		
		public ArrayList<MyShape> getGroupShape(String type){
			SQLiteDatabase db = this.getReadableDatabase();
			ArrayList<MyShape> myShape = new ArrayList<MyShape>();
			Cursor res = db.rawQuery("select * from shapeData where Type like '" + type + "'", null);
			
			if (res.moveToFirst()){
				do {
			MyShape shape = new MyShape();
			shape.setName(res.getString(res.getColumnIndex(dbShape.DATA_COLUMN_NAME)));
			shape.setPoint(res.getString(res.getColumnIndex(dbShape.DATA_COLUMN_POINT)));
			shape.setType(res.getString(res.getColumnIndex(dbShape.DATA_COLUMN_TYPE)));
			myShape.add(shape);
			}  while (res.moveToNext());
			}
			res.close();
			db.close();
			return myShape;
		}
		
		public ArrayList<PolygonOptions> getPolygon(){
			SQLiteDatabase db = this.getReadableDatabase();
			ArrayList<PolygonOptions> myPolygon = new ArrayList<PolygonOptions>();
			Cursor res = db.rawQuery("select * from shapeData where Type like '" + "polygon" + "'", null);
			
			if (res.moveToFirst()){
				do {					
			ArrayList<LatLng> listPosition = new ArrayList<LatLng>();
			PolygonOptions gonOption = new PolygonOptions();
			gonOption.strokeColor(Color.MAGENTA);
			gonOption.fillColor(0x7F00FF00);
			gonOption.visible(true);
			String position = res.getString(res.getColumnIndex(dbShape.DATA_COLUMN_POINT));
			String[] positions = position.split(";");
			for (int i=0;i<positions.length;i++){
				double x = Double.parseDouble(positions[i].split(",")[0]);
				double y = Double.parseDouble(positions[i].split(",")[1]);
				
			
				LatLng loca = new LatLng(x,y);
				listPosition.add(loca);
			}
			gonOption.addAll(listPosition);
			myPolygon.add(gonOption);
			}  while (res.moveToNext());
			}
			res.close();
			db.close();
			return myPolygon;
		}
}
